package pam.web;

import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpSession;

import pam.core.IFile;
import pam.core.XMLObject;
import pam.security.Permission;

/**
 * 应用层服务程序(pamlet)，一个pamlet负责一个功能的实现。pamlet的查找在IPamletFactory中实现。
 */
public abstract class AbstractPamlet {

	private Parameters map;

	private Map cache;
	private Map components;
	private String loginUsername;
	private HttpSession session;

	final void setParameters(Parameters map) {
		this.map = map;
	}

	public final String getName() {
		return (map != null ? map.getString(IPamletFactory.pamletID) : "");
	}

	final XMLObject execute() throws WebException {

		XMLObject result;
		result = run();
		return result;
	}

	protected final String getParameter(String key) throws WebException {
		if (key == null) {
			throw new WebException("参数为空值!");
		}
		return this.map.getString(key);
	}

	protected final String[] getParameters(String key) throws WebException {
		if (key == null) {
			throw new WebException("参数为空值!");
		}
		return this.map.getStringArray(key);
	}

	protected final IFile getFile(String key) throws WebException {
		if (key == null) {
			throw new WebException("参数为空值!");
		}
		return this.map.getFile(key);
	}

	protected final byte[] getBytes(String key) throws WebException {
		if (key == null) {
			throw new WebException("参数为空值!");
		}
		return this.map.getBytes(key);
	}

	protected abstract XMLObject run() throws WebException;

	protected final Context getContext() throws NamingException {
		InitialContext context = new InitialContext();
		return context;
	}

	final void setCache(Map cache) {
		this.cache = cache;
	}

	final void setComponents(Map components) {
		this.components = components;
	}

	final void setLoginUsername(String username) {
		this.loginUsername = username;
	}

	protected String getLoginUsername() {
		return this.loginUsername;
	}

	final void setSession(HttpSession session) {
		this.session = session;
	}

	protected HttpSession getSession() {
		return this.session;
	}

	protected Object getCachedObject(Object key) throws CacheException {
		if (cache == null) {
			throw new CacheException("缓存没有初始化!");
		}

		return cache.get(key);
	}

	protected void cacheObject(Object key, Object value) throws CacheException {
		if (cache == null) {
			throw new CacheException("缓存没有初始化!");
		}

		cache.put(key, value);
	}

	protected void removeCachedObject(Object key) throws CacheException {
		if (cache == null) {
			return;
		}

		cache.remove(key);
	}

	protected void removeCachedList(Object key) throws CacheException {
		if (cache == null) {
			return;
		}

		cache.remove(key);
	}

	protected Object getCachedList(Object key) throws CacheException {
		if (cache == null) {
			throw new CacheException("缓存没有初始化!");
		}

		return cache.get(key);
	}

	protected void cacheList(Object key, Object value) throws CacheException {
		if (cache == null) {
			throw new CacheException("缓存没有初始化!");
		}

		cache.put(key, value);
	}

	final protected Object getComponent(String jndiName)
			throws ComponentNotFoundException {

		if (components == null) {
			throw new ComponentNotFoundException("组件没有初始化!");
		}

		Object component = components.get(jndiName);

		if (component == null) {
			throw new ComponentNotFoundException("组件没有找到!");
		}

		return component;

	}

	public abstract Permission getPermission();

}
